package com.dao;

import com.beans.MemberInfo;
import com.beans.PageInfo;
import com.jdbc.DBUtil;
import com.utils.StrUtil;

import java.util.List;

public class MemberDao {
    public int getMemberCount(String memberNo, String beginDate, String endDate) {
        String sql="select count(*) from memberInfo where 1=1 ";

        if(!StrUtil.isNullOrEmpty(memberNo)){
            sql+=" and memberNo='"+memberNo+"' ";
        }
        if(!StrUtil.isNullOrEmpty(beginDate)){
            sql+=" and registerDate>='"+beginDate+"' ";
        }
        if(!StrUtil.isNullOrEmpty(endDate)){
            sql+=" and registerDate <='"+endDate+" 23:59:59' ";
        }

        Long  count= DBUtil.getScalar(sql);

        return  Integer.parseInt(count+"");
    }

    public List<MemberInfo> getMemberList(String memberNo, String beginDate, String endDate, PageInfo pageInfo) {
        String sql="select * from memberInfo where 1=1 ";

        if(!StrUtil.isNullOrEmpty(memberNo)){
            sql+=" and memberNo='"+memberNo+"' ";
        }
        if(!StrUtil.isNullOrEmpty(beginDate)){
            sql+=" and registerDate>='"+beginDate+"' ";
        }
        if(!StrUtil.isNullOrEmpty(endDate)){
            sql+=" and registerDate <='"+endDate+" 23:59:59' ";
        }

        sql+=" order by registerDate desc limit ? , ? ";

        return DBUtil.getList(sql, MemberInfo.class,pageInfo.getBeginRow(),pageInfo.getPageSize());
    }

    public int del(Integer id) {
        String sql="delete from memberInfo where id=?";
        return DBUtil.update(sql,id);
    }

    public MemberInfo getMemeberById(int memberId) {
        String sql="select * from memberInfo where id=?";
        return DBUtil.getSingleObject(sql,MemberInfo.class,memberId);
    }
}
